import sys

from elasticsearch_dsl.connections import connections

import tornado.ioloop
import tornado.web
import tornado.locale
import tornado.httpserver
from tornado.options import options, parse_command_line

from view import (
    UploadHandler,
    QueryHandler,
)

from settings import load_configs
from secrets import load_secrets

from ESMapping import Images

load_configs()
load_secrets()
parse_command_line()

connections.create_connection(hosts=[options.es_host], timeout=20)
Images.init()

application = tornado.web.Application([
    (r'/api/_search', QueryHandler),
    (r'/api/_fs/(?P<filename>.*)', UploadHandler),
    (r"/static/(.*)", tornado.web.StaticFileHandler,
     {'path': options.static_dir})],
    debug=options.debug,
    static_path=options.static_dir,
)
server = tornado.httpserver.HTTPServer(application, xheaders=True)
server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
